package cn.kgc.musicserver.config;

import cn.kgc.musicserver.interceptors.UserInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * @author XiaoMo
 * @version 1.0
 * @date 2025/6/18 15:23
 */
@RequiredArgsConstructor
@Configuration
public class WebConfig implements WebMvcConfigurer {
    private final UserInterceptor userInterceptor;
    //歌手文件夹
    @Value("${file.path}")
    private String path;
    //注入拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
      //  拦截所有请求路径交给userInterceptor来验证是否登录（放行登录方法和所有查询接口）
      registry
              .addInterceptor(userInterceptor)
              .addPathPatterns("/**")
              .excludePathPatterns(
                      "/admin/login","/admin/register",
                      "/r/**",
                      // 放行所有查询接口（GET请求）
                      "/song/**",
                      "/singer/**",
                      "/song-list/**",
                      "/consumer/**",
                      "/comment/**",
                      "/rank/**",
                      "/video/**",
                      "/title/**",
                      "/list-song/**"
              );
    }
    //资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String file_path = "file:" + path + File.separator;
        //映射本地音乐
        registry.addResourceHandler("/r/song/**")
                .addResourceLocations(file_path+"song"+ File.separator);

        //映射本地图片
        registry.addResourceHandler("/r/img/**")
                .addResourceLocations(file_path+"images"+ File.separator);

        //映射本地视频
        registry.addResourceHandler("/r/video/**")
                .addResourceLocations(file_path+"video"+ File.separator);

    }
    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*");
    }
}
